// onselect year
function change_age_month_box() {
	var age_year = document.getElementById('age_year');
	var age_year_label = document.getElementById('age_year_label');
	var age_month = document.getElementById('age_month');
	// remove the month_box when year is > 3.
	if (age_year.value > 3) {
		$('label[for="age_month"]').hide();
		$('#age_month').hide();
	} else {
		$('label[for="age_month"]').show();
		$('#age_month').show();
		if (age_year.value == 0) {
			age_month.options[0].text = "<1";
		} else {
			age_month.options[0].text = "0";
		}
	}
	// years and year
	if (age_year.value > 1) {
		age_year_label.innerHTML = "years";
	} else {
		age_year_label.innerHTML = "year";
	}
}

// onselect month
function change_age_month_label() {
	var age_month = document.getElementById('age_month');
	var age_month_label = document.getElementById('age_month_label');
	
	if (age_month.value > 1) {
		age_month_label.innerHTML = "months";
	} else {
		age_month_label.innerHTML = "month";
	}
}


// onselect price choice
// remove the price box if user wants to give the item free.
function change_price_box() {
	if (document.getElementById('price_free').checked == true) {
		$('#item_price').hide();
		document.getElementById('item_price').value = 0;
	} else {
		$('#item_price').show();
	}
}	

// onselect category
function change_sub_category_box() {
	var category_id = document.getElementById('category_id');
	var subcat = document.getElementById('sub_category_id');

	// reveal the sub_category_box only when category is not miscellaneous
	subcat.options.length = 0;
	var count = 0 ;
	for (var i = 0; i < sub_categories.length; ++i) {
		if (sub_categories[i].category_id == category_id.value) {
			subcat.options[count] = new Option(sub_categories[i].sub_category_name,
											sub_categories[i].sub_category_id);
			++count;
		}
	}
	if (count == 0) {
		$('label[for="sub_category"]').hide();
		$('#sub_category_id').hide();
	} else {
		$('label[for="sub_category"]').show();
		$('#sub_category_id').show();;
	}		
	
	// change the default image if no image is selected.
	var item_pic_name = document.getElementById('item_pic_name');
	var image_preview =  document.getElementById('image_preview');
	var default_image_array = ["miscellaneous_default.jpg", "appliances_default.jpg",
			"apparel_default.jpg", "electronics_default.jpg", "equipment_and_tools_default.jpg",
			"furniture_default.jpg", "games_default.jpg", "handheld_devices_default.jpg",
			"paper_default.jpg", "stationery_default.jpg"];
	if (jQuery.inArray(item_pic_name.value,default_image_array) >= 0) {
		while (image_preview.hasChildNodes()) {
			image_preview.removeChild(image_preview.lastChild);
		}
		
		item_pic_name.value = default_image_array[category_id.value-1];
		
		var image = document.createElement('img');
		image.setAttribute('src',base_url + 'resources/offer_images_thumbnail/' + default_image_array[category_id.value-1]);
		image_preview.appendChild(image);
	}
}

$(function(){
	change_sub_category_box();
	change_age_month_box();
	change_age_month_label();
	change_price_box();
	var subcat = document.getElementById('sub_category_id');
	// display the subcategory
	for (var i = 0; i < subcat.options.length; ++i) {
		if (subcat.options[i].value == sub_cat_id) {
			subcat.options[i].selected = "1";
		}
	}
});
	